package com.iwhalecloud.bss.kite.cucc.common.callclient;

import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.common.callclient.constant.ICallClientEnum;
import com.iwhalecloud.bss.kite.common.callclient.service.IOrderQueryService;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.entity.InfService;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.callclient.service.ICuccQueryInfService;
import com.iwhalecloud.bss.kite.cucc.common.util.KiteHttpUtils;
import com.ztesoft.bss.common.util.SpringUtil;

import java.util.Map;

/**
 * <Description> http服务调用 <br>
 *
 * @author huang.caibiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @date 2020-02-25 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.common.callclient <br>
 */
public abstract class HttpCallClient {


    public static Map post(ICallClientEnum callClientEnum, Map<String, Object> busiMap) {
        return post(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static Map post(ICallClientEnum callClientEnum, Map<String, Object> busiMap, Map<String, String> headerMap) {
        return post(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, headerMap);
    }

    public static Map post(String centerCode, String serviceCode, Map<String, Object> busiMap) {
        return post(centerCode, serviceCode, busiMap, null);
    }

    public static Map post(String centerCode, String serviceCode, Map<String, Object> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        ICuccQueryInfService cuccQueryInfService = SpringUtil.getBean(ICuccQueryInfService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        String rsp;
        if (KiteMapUtils.isNotEmpty(headerMap)) {
            rsp = KiteHttpUtils.post(cuccQueryInfService.getUrlByType(infService), busiMap, headerMap);

        }
        else {
            rsp = KiteHttpUtils.post(cuccQueryInfService.getUrlByType(infService), busiMap);
        }

        return JSON.parseObject(rsp, Map.class);
        /*if (infService.getClassName().indexOf("/goods/goodsInfo") >= 0) {
            return KiteMapUtils.getMap(result, "data");
        }
        if ((result.containsKey("success") && !"true".equals(KiteMapUtils.getString(result, "success")))
                || (result.containsKey("code") && !"200".equals(KiteMapUtils.getString(result, "code")))) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(infService.getSvcCode(), infService.getSvcDesc(), KiteMapUtils.getString(result, "message"));
        }*/
        //return KiteMapUtils.getMap(result, "data");
    }


    public static Map get(ICallClientEnum callClientEnum, Map<String, Object> busiMap) {
        return get(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static Map get(ICallClientEnum callClientEnum, Map<String, Object> busiMap, Map<String, String> headerMap) {
        return get(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, headerMap);
    }

    public static Map get(String centerCode, String serviceCode, Map<String, Object> busiMap) {
        return get(centerCode, serviceCode, busiMap, null);
    }

    public static Map get(String centerCode, String serviceCode, Map<String, Object> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
    	IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
    	ICuccQueryInfService cuccQueryInfService = SpringUtil.getBean(ICuccQueryInfService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        String rsp;
        if (KiteMapUtils.isNotEmpty(headerMap)) {
            rsp = KiteHttpUtils.get(cuccQueryInfService.getUrlByType(infService), busiMap, headerMap);
        }
        else {
            rsp = KiteHttpUtils.get(cuccQueryInfService.getUrlByType(infService), busiMap);
        }
        return JSON.parseObject(rsp, Map.class);
    }


    public static String postString(ICallClientEnum callClientEnum, Map<String, Object> busiMap) {
        return postString(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static String getString(ICallClientEnum callClientEnum, Map<String, Object> busiMap) {
        return getString(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    private static String getString(String centerCode, String serviceCode, Map<String, Object> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
    	IOrderQueryService orderQueryService  = SpringUtil.getBean(IOrderQueryService.class);
    	ICuccQueryInfService cuccQueryInfService = SpringUtil.getBean(ICuccQueryInfService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }
        String rsp;
        if (KiteMapUtils.isNotEmpty(headerMap)) {
            rsp = KiteHttpUtils.get(cuccQueryInfService.getUrlByType(infService), busiMap, headerMap);
        }
        else {
            rsp = KiteHttpUtils.get(cuccQueryInfService.getUrlByType(infService), busiMap);
        }
        return rsp;
    }

    public static String postString(String centerCode, String serviceCode, Map<String, Object> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
    	IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
    	ICuccQueryInfService cuccQueryInfService = SpringUtil.getBean(ICuccQueryInfService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        String rsp;
        if (KiteMapUtils.isNotEmpty(headerMap)) {
            rsp = KiteHttpUtils.post(cuccQueryInfService.getUrlByType(infService), busiMap, headerMap);

        }
        else {
            rsp = KiteHttpUtils.post(cuccQueryInfService.getUrlByType(infService), busiMap);
        }
        if (KiteStringUtils.isEmpty(rsp)) {
            return "";
        }
        /*Map<String, Object> result = JSON.parseObject(rsp, Map.class);
        if("SUCCESS".equals(KiteMapUtils.getString(result, "message")) || "0".equals(KiteMapUtils.getString(result, "status"))) {
            return KiteMapUtils.getString(result, "data");
        }
        if (!"true".equals(KiteMapUtils.getString(result, "success"))) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(infService.getSvcCode(), infService.getSvcDesc(), KiteMapUtils.getString(result, "message"));
        }
        return KiteMapUtils.getString(result, "data");*/
        return rsp;

    }

}
